package com.tjetc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.common.Result;
import com.tjetc.domain.Bed;
import com.tjetc.domain.Room;
import com.tjetc.service.BedService;
import com.tjetc.service.RoomService;
import com.tjetc.service.impl.BedServiceImpl;
import com.tjetc.service.impl.RoomServiceImpl;
import com.tjetc.utils.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("/room/*")
@MultipartConfig
public class RoomServlet extends HttpServlet {
    private RoomService roomService=new RoomServiceImpl();
    private BedService bedService=new BedServiceImpl();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String number = request.getParameter("number");
        String curPage = request.getParameter("curPage");
        String ps = request.getParameter("pageSize");
        number=number==null?"":number;

        //接收请求参数
        //空处理:赋值给当前页码
        int currentPage = curPage == null ? 1:Integer.valueOf(curPage);
        //每页显示的条数
        int pageSize = ps == null ? 5 : Integer.parseInt(ps);
        //总记录数
        int count =roomService.getCount(number);
        PageInfo<Room> pageInfo = new PageInfo<>(currentPage, count, pageSize);
        List<Room> list=roomService.list(number,pageInfo);
        pageInfo.setList(list);
        new ObjectMapper().writeValue(response.getOutputStream(),pageInfo);

    }


    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String number=request.getParameter("number");
        String type=request.getParameter("type");
        String position=request.getParameter("position");
        int depid= Integer.valueOf(request.getParameter("depid"));

       Room room1=roomService.findByNumber(number);
        if (room1!=null){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("添加失败，病房名不能重复！"));
        }else{
            Room room=new Room();
            room.setNumber(number);
            room.setType(type);
            room.setPosition(position);
            room.setDepid(depid);
            boolean b=roomService.add(room);
            if (b){
                new ObjectMapper().writeValue(response.getOutputStream(), Result.success("添加成功"));
            }
            new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("添加失败，出异常喽！"));
        }
    }


    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String number=request.getParameter("number");
        String type=request.getParameter("type");
        String position=request.getParameter("position");
        int depid= Integer.valueOf(request.getParameter("depid"));
        int status= Integer.parseInt(request.getParameter("status"));
        Room room1=roomService.findByNumber(number);
        if (room1!=null){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("修改失败，病房名不能重复！"));
        }else{
            Room room=new Room();
            room.setId(id);
            room.setNumber(number);
            room.setType(type);
            room.setPosition(position);
            room.setDepid(depid);
            room.setStatus(status);
            boolean b=roomService.update(room);
            if (b){
                new ObjectMapper().writeValue(response.getOutputStream(), Result.success("修改成功"));
            }
            new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("修改失败"));
        }
    }


    protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id= Integer.parseInt(request.getParameter("id"));
        List<Bed> list=bedService.findByRid(id);
        for (Bed bed : list) {
            System.out.println("bed = " + bed);
        }
        if (list.isEmpty()){
            boolean b=roomService.del(id);
            if (b){
                new ObjectMapper().writeValue(response.getOutputStream(), Result.success("删除成功"));
            }
            new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("删除失败"));

        }else{
            new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("删除失败，请先删除绑定的病床信息！"));
        }
    }


    protected void list1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Room> list=roomService.list1();
        new ObjectMapper().writeValue(response.getOutputStream(),list);
    }
}
